;(function(win) {
  let doc = win.document;
  let docEl = doc.documentElement;
  let tid;

  function refreshRem() {
    let width = docEl.getBoundingClientRect().width;
    if (width > 640) {
      width = 640;
    }
    let rem = width / 6.4;
    docEl.style.fontSize = rem + 'px';
  }
  win.addEventListener('resize', () => {
    clearTimeout(tid);
    tid = setTimeout(refreshRem, 300)
  }, false);
  win.addEventListener('pageshow', e => {
    if (e.persisted) {
      clearTimeout(tid)
      tid = setTimeout(refreshRem, 300)
    }
  }, false);
  refreshRem()
})(window)